package admin

import (
	"chaowan/models"
	"chaowan/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Provinces(ctx *gin.Context) {

	var citys []models.City
	citys = service.Citylist()

	result := getListGoods(citys, 0, 1)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    result,
		"msg":     "ok",
	})
	return
}

func getListGoods(stuAll []models.City, pid int64, lev int64) []models.City {
	var goodArr []models.City
	for _, v := range stuAll {
		if v.Pid == pid {
			// 这里可以理解为每次都从最原始的数据里面找出相对就的ID进行匹配，直到找不到就返回
			child := getListGoods(stuAll, v.ID, lev+1)
			node := models.City{
				ID:    v.ID,
				Name:  v.Name,
				Type:  lev,
				First: v.First,
				Remen: v.Remen,
				List:  child,
			}
			goodArr = append(goodArr, node)
		}
	}
	return goodArr
}
